$(function () {

    // TODO
    Vue.use(VeeValidate);

    new Vue({
        el: '#app',
        data: {
            show: false,
            selectStu:false,
            // 0 新增
            // 1 修改
            modalType: 0,
            showClass: false,
            condition: {
                type: null,
                pattern: null
            },
            page: {},
            item: {
                student: {
                    classes: {}
                },
                type: {
                    value: null
                },
                pattern: {
                    value: null
                }
            },
            items: [],
            types: [],
            patterns: []
        },
        mounted: function () {
        	$("input.date").datetimepicker({
        		format: 'yyyy-mm-dd',
        		minView: 2,
        		autoclose: true,
        		language: "zh-CN"
        	});
        	
            $.showloading();
            // 获取考试类型
            var p1 = this.getDicts(8, rows => {
                this.types = rows;
            });
            // 获取考试方式
            var p2 = this.getDicts(9, rows => {
                this.patterns = rows;
            });

            Promise.all([p1, p2]).then(result => {
                $.hideloading();
            }).catch(result => {
                $.hideloading();
                bootbox.alert({
                    message: '系统错误',
                    size: 'small'
                });
            });
            this.serach();
        },
        methods: {
            open: function (type, id) {
                this.modalType = type;
                this.show = true;
                // 初始化item
                this.item = {
                    student: {
                        classes: {}
                    },
                    type: {
                        value: null
                    },
                    pattern: {
                        value: null
                    }
                };
                
                setTimeout(() => {
                	$('input.date[name="scoreDate"]').datetimepicker({
                		format: 'yyyy-mm-dd',
                		minView: 2,
                		autoclose: true,
                		language: "zh-CN"
                	});
                	$('input.date[name="scoreDate"]').change(() => {
                		this.item.date = $('input.date[name="scoreDate"]').val();
                	});
                }, 350);
                
                // 根据类型判断是否获取成绩信息
                if (type != 1) {
                	// 设置默认值
                	this.item.passScore = 60;
                	this.item.totalScore = 100;
                    return;
                }
                $.showloading();
                this.$http.get('score/' + id + '/info').then(res => {
                    var rsp = res.data;
                    if ('0' !== rsp.err) {
                        bootbox.alert({
                            message: rsp.msg,
                            size: 'small'
                        });
                    }
                    
                    this.item = rsp.data;
                    // 转换成日期字符串
                    this.item.date = this.formatDate(this.item.date);
                    $('input.date[name="scoreDate"]').val(this.item.date);
                }).catch(() => {
                    bootbox.alert({
                        message: '系统错误',
                        size: 'small'
                    });
                }).finally(() => {
                    $.hideloading();
                });
            },
            serach: function () {
                $.showloading();
                this.$http.post('score/list', {
                    condition: {
                        studentId: this.condition.studentId,
                        className: this.condition.className,
                        type: this.condition.type,
                        pattern: this.condition.pattern,
                        // 暂时用jquery的代替
                        startDate: $('input[name="startDate"]').val(),
                        endDate: $('input[name="endDate"]').val(),
                        minPoint: this.condition.minPoint,
                        maxPoint: this.condition.maxPoint
                    },
                    page: this.page
                }).then(res => {
                    var rsp = res.data;
                    if ('0' !== rsp.err) {
                        bootbox.alert({
                            message: rsp.msg,
                            size: 'small'
                        });
                        return;
                    }

                    this.items = rsp.rows;
                    this.page = rsp.page;
                    $.hideloading();
                }).finally(() => {
                    $.hideloading();
                });
            },
            // 根据类型获取字典列表
            getDicts: function (type, func) {
                return this.$http.get('dict/list', {
                    params: {
                        type: type
                    }
                }).then(res => {
                    func(res.data.rows);
                });
            },
            // 新增成绩信息
            add: function () {
            	this.$validator.validateAll();
            	if (this.$validator.errorBag.any()) {
                	return ;
                }
            	
                $.showloading();
                this.$http.post('score/add', this.item)
                    .then(res => {
                        var rsp = res.data;
                        bootbox.alert({
                            message: rsp.msg,
                            size: 'small'
                        });
                        if ('0' !== rsp.err) {
                            return;
                        }
                        
                        this.show = false;
                        this.serach();
                    }).catch(() => {
                        bootbox.alert({
                            message: '系统错误',
                            size: 'small'
                        });
                    }).finally(() => {
                        $.hideloading();
                    });
            },
            // 编辑成绩信息
            edit: function () {
            	this.$validator.validateAll();
                if (this.$validator.errorBag.any()) {
                	return ;
                }
                
                // 获取元素值
                this.item.date = $('input.date[name="scoreDate"]').val();
                $.showloading();
                this.$http.post('score/edit', this.item)
                    .then(res => {
                        var rsp = res.data;
                        bootbox.alert({
                            message: rsp.msg,
                            size: 'small'
                        });

                        if ('0' !== rsp.err) {
                            return;
                        }

                        this.serach();
                        this.show = false;
                    }).catch(() => {
                        bootbox.alert({
                            message: '系统错误',
                            size: 'small'
                        });
                    }).finally(() => {
                        $.hideloading();
                    });
            },
            // 删除成绩信息
            del: function (id) {
                bootbox.confirm({
                    message: "确认删除编号为" + id + "的考勤机？",
                    buttons: {
                        confirm: {
                            label: '是的',
                            className: 'btn-success'
                        },
                        cancel: {
                            label: '取消',
                            className: 'btn-danger'
                        }
                    },
                    callback: result => {
                        if (!result) {
                            return;
                        }
                        $.showloading();
                        this.$http.get('score/' + id + '/del')
                            .then(res => {
                                var rsp = res.data;
                                bootbox.alert({
                                    message: rsp.msg,
                                    size: 'small'
                                });
                                if ('0' == rsp.err) {
                                    this.serach();
                                    return;
                                }
                            }).catch(() => {
                                bootbox.alert({
                                    message: '系统错误',
                                    size: 'small'
                                });
                            }).finally(() => {
                                $.hideloading();
                            });
                    }
                });
            },
            openTab: function (url, title) {
                window.parent.tabUtil.add(url, title, true);
            },
            exportExcel: function() {
            	
            	var getVal = function(val) {
            		if (undefined == val) {
            			return '';
            		}
            		
            		return val;
            	}
            	
            	window.location = "score/export?studentId=" + getVal(this.condition.studentId) + 
            		"&className=" + getVal(this.condition.className) + 
            		"&type=" + getVal(this.condition.type) +
            		"&pattern=" + getVal(this.condition.pattern) +
            		"&startDate" + $('input[name="startDate"]').val() +
            		"&endDate" + $('input[name="endDate"]').val() +
            		"&minPoint" + getVal(this.condition.minPoint) +
            		"&maxPoint" + getVal(this.condition.maxPoint);
            },
            formatDate: function(val) {
            	return moment(val).format('YYYY-MM-DD')
            }
        }
    });
    
    
});